package com.example.svnkit_low_api.util;

import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.io.diff.SVNDeltaGenerator;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
import java.io.ByteArrayInputStream;
import java.net.InetAddress;

/**
 * https://wiki.svnkit.com/Committing_To_A_Repository
 * @author Shenshuxin
 * @version 1.0
 * @apiNote 在官网学习实现demo
 */
public class SVNLowUtil {
    private String userName;
    private String userPassword;
    private String repoUrl;
    private ISVNEditor editor;

    /**
     * 构造方法 且赋值
     * @param userName 用户名字
     * @param userPassword 用户密码
     * @param repoUrl 仓库url
     */
    public SVNLowUtil(String userName, String userPassword, String repoUrl) {
        this.userName = userName;
        this.userPassword = userPassword;
        this.repoUrl = repoUrl;
        this.editor = init();
    }

    /**
     * 默认选择http方式
     * 协议
     * SVNRepositoryFactory 3种实现
     *
     * svn://
     * SVNRepositoryFactoryImpl
     *
     * http://
     * DAVRepositoryFactory
     *
     * 文件：///
     * FSRepositoryFactory
     */
    private ISVNEditor init(){
        //创建驱动程序
        DAVRepositoryFactory.setup();

        //创建存储库
        try {
            SVNRepository svnRepository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(repoUrl));
            ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(userName, userPassword);
            svnRepository.setAuthenticationManager(authManager);

//            svnRepository.setLocation();

            long latestRevision = svnRepository.getLatestRevision();
            return svnRepository.getCommitEditor("这是我的日志信息", null);

//            return svnRepository.getCommitEditor("日志消息", null /*locks*/, true /*keepLocks*/, null);
        } catch (SVNException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * svn://localhost/rep
     * test
     * test/file.txt
     *
     * 明白 root:svn://localhost/rep
     * @param dirPath test
     * @param filePath test/file.txt
     * @param data 字节数组
     * @return
     * @throws SVNException
     */
    public SVNCommitInfo addDir( String dirPath,
                                String filePath, byte[] data) throws SVNException {
        editor.openRoot(-1);

        // dirPath是相对于根目录的。
        editor.addDir(dirPath, null, -1);
        editor.addFile(filePath, null, -1);
        editor.applyTextDelta(filePath, null);

        SVNDeltaGenerator deltaGenerator = new SVNDeltaGenerator();
        String checksum = deltaGenerator.sendDelta(filePath, new ByteArrayInputStream(data), editor, true);

        editor.closeFile(filePath, checksum);

        editor.closeDir();

        editor.closeDir();
        /*
         * This is the final point in all editor handling. Only now all that new
         * information previously described with the editor's methods is sent to
         * the server for committing. As a result the server sends the new
         * commit information.
         */
        return editor.closeEdit();

    }
}
